package org.wmq.shopmanager.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.wmq.shopmanager.mapper.ProductMapper;
import org.wmq.shopmanager.service.ProductDetailService;
import org.wmq.shopmanager.service.ProductService;
import org.wmq.shopmanager.service.ProductSkuService;
import org.wmq.shopmodel.DTO.product.ProductQueryDTO;
import org.wmq.shopmodel.Entity.product.Product;
import org.wmq.shopmodel.Entity.product.ProductDetail;
import org.wmq.shopmodel.Entity.product.ProductSku;

import java.util.List;

@Service
@RequiredArgsConstructor
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements ProductService {
    private final ProductMapper productMapper;
    private final ProductSkuService productSkuService;
    private final ProductDetailService productDetailService;

    @Override
    public IPage<Product> getPageItem(ProductQueryDTO productQueryDTO) {
        Page<Product> page = new Page<>(productQueryDTO.getPageNo(), productQueryDTO.getPageSize());
        LambdaQueryWrapper<Product> queryWrapper = new LambdaQueryWrapper<>();
        if (productQueryDTO.getBrandId() != null){
            queryWrapper.eq(Product::getBrandId,productQueryDTO.getBrandId());
        }
        if (productQueryDTO.getCategory1Id() != null){
            queryWrapper.eq(Product::getCategory1Id,productQueryDTO.getCategory1Id());
        }
        if (productQueryDTO.getCategory2Id() != null){
            queryWrapper.eq(Product::getCategory2Id,productQueryDTO.getCategory2Id());
        }
        if (productQueryDTO.getCategory3Id() != null){
            queryWrapper.eq(Product::getCategory3Id,productQueryDTO.getCategory3Id());
        }
        queryWrapper.orderByDesc(Product::getId);
        return this.page(page,queryWrapper);
    }

    @Override
    @Transactional
    public void saveProduct(Product product) {
        this.productMapper.insert(product);
        List<ProductSku> productSkuList = product.getProductSkuList();
        for (ProductSku productSku : productSkuList) {
            productSku.setProductId(product.getId());
            productSku.setSkeName(product.getName() + productSku.getSkuSpec());
        }
        this.productSkuService.saveBatch(productSkuList);

        ProductDetail productDetail = new ProductDetail();
        productDetail.setProductId(product.getId());
        productDetail.setImageUrls(product.getDetailsImageUrls());
        this.productDetailService.save(productDetail);
    }

    @Override
    public Product getProductAllFieldById(Long id) {
        Product result = this.getProductWithCategoryNamesById(id);
        LambdaQueryWrapper<ProductSku> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ProductSku::getProductId, id);
        result.setProductSkuList(this.productSkuService.list(queryWrapper));
        LambdaQueryWrapper<ProductDetail> queryWrapper2 = new LambdaQueryWrapper<>();
        queryWrapper2.eq(ProductDetail::getProductId,id)
                        .select(ProductDetail::getImageUrls);
        result.setDetailsImageUrls(this.productDetailService.getOne(queryWrapper2).getImageUrls());
        return result;
    }

    @Override
    @Transactional
    public void updateProductById(Product product) {
        this.updateById(product);
        LambdaQueryWrapper<ProductSku> productSkuQueryWrapper = new LambdaQueryWrapper<>();
        productSkuQueryWrapper.eq(ProductSku::getProductId,product.getId());
        this.productSkuService.remove(productSkuQueryWrapper);
        List<ProductSku> productSkuList = product.getProductSkuList();
        if (productSkuList != null && !productSkuList.isEmpty()){
            this.productSkuService.saveBatch(productSkuList);
        }
        LambdaQueryWrapper<ProductDetail> productDetailQueryWrapper = new LambdaQueryWrapper<>();
        productDetailQueryWrapper.eq(ProductDetail::getProductId,product.getId());
        this.productDetailService.remove(productDetailQueryWrapper);
        if (product.getDetailsImageUrls() != null){
            ProductDetail productDetail = new ProductDetail();
            productDetail.setImageUrls(product.getSliderUrls());
            this.productDetailService.save(productDetail);
        }
    }

    @Override
    @Transactional
    public void removeProductById(Long id) {
        this.productMapper.deleteById(id);
        LambdaQueryWrapper<ProductSku> productSkuQueryWrapper = new LambdaQueryWrapper<>();
        productSkuQueryWrapper.eq(ProductSku::getProductId,id);
        this.productSkuService.remove(productSkuQueryWrapper);
        LambdaQueryWrapper<ProductDetail> productDetailQueryWrapper = new LambdaQueryWrapper<>();
        productDetailQueryWrapper.eq(ProductDetail::getProductId,id);
        this.productDetailService.remove(productDetailQueryWrapper);
    }

    private Product getProductWithCategoryNamesById(Long id){
        return this.productMapper.selectWithCategoryNamesById(id);
    }
}
